package com.sanji.sjmallmodule.order;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/19.
 */

public class AliPayWebViewActivity extends BaseActivity {

    @BindView(R.id.alipay_webview)
    WebView aliPayWebView;

    private String aliUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alipay_webview);
        ButterKnife.bind(this);

        aliUrl = getIntent().getStringExtra("aliUrl");

        initWebView();
    }

    private void initWebView() {

        WebSettings webSettings = aliPayWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
//        webSettings.setAllowFileAccess(true);// 设置可以访问文件
//        webSettings.setBuiltInZoomControls(true);// 设置支持缩放
//        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDomStorageEnabled(true);
//        webSettings.setGeolocationEnabled(true);


        //网页开始、结束监听
        aliPayWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("platformapi/startapp")) {
                    startAlipayActivity(url);
                    // android  6.0 两种方式获取intent都可以跳转支付宝成功,7.1测试不成功
                } else if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
                        && (url.contains("platformapi") && url.contains("startapp"))) {
                    startAlipayActivity(url);
                }else{
                    aliPayWebView.loadUrl(url);
                }
                return true;
            }

        });

        aliPayWebView.loadUrl(aliUrl);
    }


    private void startAlipayActivity(String url) {
        Intent intent;
        try {
            intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setComponent(null);
            startActivity(intent);
            setResult(RESULT_OK,null);
            finish();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public boolean parseSchem(String url){
//        if (url.contains("platformapi/startapp")){
//            mHandler.removeCallbacks();
//        }
//    }


//    private Handler mHandler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//
//        }
//    };
}
